
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    private static Scanner consola = new Scanner(System.in);
    private static StringTokenizer st;
    private static String[][] imagen;
    private static StringBuilder sb = new StringBuilder();
    private static boolean hayImagenCreada = false;

    public static void main(String args[]) {
        while (consola.hasNextLine()) {
            st = new StringTokenizer(consola.nextLine());
            if (st.hasMoreTokens()) {
                switch (st.nextToken().charAt(0)) {

                    //I M N Creates a new table M×N. All the pixels are colored in white (O).
                    case 'I':
                    case 'i':
                        crearImagen(Integer.parseInt(st.nextToken()),
                                Integer.parseInt(st.nextToken()));
                        limpiarImagen();
                        hayImagenCreada = true;
                        break;

                    /*C Clears the table.
                     * The size remains the same.
                     * All the pixels became white (O).
                     */
                    case 'C':
                    case 'c':
                        if (hayImagenCreada) {
                            limpiarImagen();
                        }
                        break;

                    //L X Y C Colors the pixel with coordinates (X,Y) in colour C.
                    case 'L':
                    case 'l':
                        if (hayImagenCreada) {
                            l(Integer.parseInt(st.nextToken()),
                                    Integer.parseInt(st.nextToken()),
                                    st.nextToken());
                        }
                        break;

                    //V X Y1 Y2 C Draws the vertical segment in the column X 
                    //between the rows Y1 and Y2 inclusive in colour C.
                    case 'V':
                    case 'v':
                        if (hayImagenCreada) {
                            v(Integer.parseInt(st.nextToken()),
                                    Integer.parseInt(st.nextToken()),
                                    Integer.parseInt(st.nextToken()),
                                    st.nextToken());
                        }
                        break;

                    //H X1 X2 Y C Draws the horizontal segment in the row Y
                    //between the columns X1 and X2 inclusive in colour C.
                    case 'H':
                    case 'h':
                        if (hayImagenCreada) {
                            h(Integer.parseInt(st.nextToken()),
                                    Integer.parseInt(st.nextToken()),
                                    Integer.parseInt(st.nextToken()),
                                    st.nextToken());
                        }
                        break;

                    //K X1 Y1 X2 Y2 C Draws the filled rectangle in colour C.
                    //(X1,Y1) is the upper left corner,
                    //(X2,Y2) is the lower right corner of the rectangle.
                    case 'K':
                    case 'k':
                        if (hayImagenCreada) {
                            k(Integer.parseInt(st.nextToken()),
                                    Integer.parseInt(st.nextToken()),
                                    Integer.parseInt(st.nextToken()),
                                    Integer.parseInt(st.nextToken()),
                                    st.nextToken());
                        }
                        break;

                    /*F X Y C Fills the region with the colour C.
                     *The region R to be filled is defined as follows.
                     *The pixel (X,Y) belongs to this region. 
                     *The other pixel belongs to the region R if and only if
                     *it has the same colour as pixel (X,Y) and a common side
                     *with any pixel which belongs to this region.
                     */
                    case 'F':
                    case 'f':
                        int x,
                         y;
                        if (hayImagenCreada) {
                            f(x = (Integer.parseInt(st.nextToken()) - 1),
                                    y = (Integer.parseInt(st.nextToken()) - 1),
                                    imagen[y][x],
                                    st.nextToken());
                        }
                        break;

                    //S Name Writes the picture in the file Name.
                    case 'S':
                    case 's':
                        if (hayImagenCreada) {
                            s(st.nextToken());
                        }
                        break;

                    //X Terminates the session.
                    case 'X':
                    case 'x':
                        //imprimirImagen();
                        salir();
                        break;
                    default:
                }//FIN Switch
            }//
        }//FIN de todas las lineas
        salir();
    }

    private static void crearImagen(int mColumnas, int nFilas) {
        imagen = new String[nFilas][mColumnas];
    }

    private static void limpiarImagen() {
        for (int i = 0; i < imagen.length; i++) {
            for (int j = 0; j < imagen[0].length; j++) {
                imagen[i][j] = "O";
            }
        }
    }

    private static void l(int xColumna, int yFila, String nuevoPixel) {
        imagen[yFila - 1][xColumna - 1] = nuevoPixel;
    }

    private static void v(int xColumna, int filaY1, int filaY2,
            String nuevoPixel) {
        if (filaY1 < filaY2) {
            for (int i = filaY1 - 1; i < filaY2; i++) {
                imagen[i][xColumna - 1] = nuevoPixel;
            }
        } else {
            for (int i = filaY2 - 1; i < filaY1; i++) {
                imagen[i][xColumna - 1] = nuevoPixel;
            }
        }
    }

    private static void h(int columnaX1, int columnaX2, int filaY,
            String nuevoPixel) {
        if (columnaX1 < columnaX2) {
            for (int i = columnaX1 - 1; i < columnaX2; i++) {
                imagen[filaY - 1][i] = nuevoPixel;
            }
        } else {
            for (int i = columnaX2 - 1; i < columnaX1; i++) {
                imagen[filaY - 1][i] = nuevoPixel;
            }
        }
    }

    private static void k(int columnaX1, int filaY1, int columnaX2,
            int filaY2, String nuevoPixel) {
        for (int i = columnaX1 - 1; i < columnaX2; i++) {
            for (int j = filaY1 - 1; j < filaY2; j++) {
                imagen[j][i] = nuevoPixel;
            }
        }
    }

    private static void f(int columna, int fila, String colorOriginal, String nuevoPixel) {

        if (colorOriginal.compareTo(nuevoPixel) != 0) {
            imagen[fila][columna] = nuevoPixel;

            //Pixel de arriba
            if (fila - 1 >= 0) {
                if (imagen[fila - 1][columna].compareTo(colorOriginal) == 0) {
                    f(columna, fila - 1, colorOriginal, nuevoPixel);
                }
            }

            //Pixel de la izquierda
            if (columna - 1 >= 0) {
                if (imagen[fila][columna - 1].compareTo(colorOriginal) == 0) {
                    f(columna - 1, fila, colorOriginal, nuevoPixel);
                }
            }

            //Pixel de la derecha
            if (columna + 1 < imagen[fila].length) {
                if (imagen[fila][columna + 1].compareTo(colorOriginal) == 0) {
                    f(columna + 1, fila, colorOriginal, nuevoPixel);
                }
            }

            //Pixel de abajo
            if (fila + 1 < imagen.length) {
                if (imagen[fila + 1][columna].compareTo(colorOriginal) == 0) {
                    f(columna, fila + 1, colorOriginal, nuevoPixel);
                }
            }
        }
    }

    private static void s(String name) {
        sb.append(name);
        sb.append("\n");
        imprimirImagen();
    }

    private static void imprimirImagen() {
        for (int i = 0; i < imagen.length; i++) {
            for (int j = 0; j < imagen[0].length; j++) {
                sb.append(imagen[i][j]);
            }
            sb.append("\n");
        }
    }

    private static void salir() {
        System.out.print(sb.toString());
        System.exit(0);
    }
}